<?php

namespace App\Http\Controllers\Api;

use App\Models\MemberUnlockFunc;
use App\Models\Msg;
use App\Repositories\Api\AdviceRepository;
use App\Repositories\Api\MemberRepository;
use App\Repositories\MsgRepository;
use Cache;

class MessageController extends MiniAppController
{
    private $memberRep = NULL,
        $msgRep = NULL;

    public function __construct(
        MemberRepository $memberRep,
        MsgRepository    $msgRep
    )
    {
        parent::__construct();
        $this->memberRep = $memberRep;
        $this->msgRep = $msgRep;
    }

    public function list()
    {
        $params = request()->all();
        if ($params['type'] == "love") {
            $params['type'] = 17;
        }

        $params['send_uid'] = -1;
        $res = $this->msgRep->getList($params, $this->pageSize, $this->pageNum);

        if ($this->pageNum) {
            $res['system_message'] = Msg::where("df_uid", "=", UID)
                ->where(function ($q) {
                    return $q->whereNull("uid")->orWhere("uid", "=", 0);
                })
                ->orderby("id", "desc")->first();
        }

        if (request()->get("t")) {
            $res = $this->msgRep->getPages($params, $this->pageSize, $this->pageNum);
            return $this->apiResponse->success($res);
        }

        if ($this->pageNum == 1) {
            $res['unlock_type'] = unlock_type();
            $res['reply_amount'] = (new AdviceRepository())->getUnreadAmount(UID);
            $res['member'] = (new MemberRepository())->getSimpleMember(UID, 2);
        }

        return $this->apiResponse->success($res);
    }

    public function info()
    {
        $msg_id = request()->get("msg_id");
        if (!$msg_id)
            return retFail("数据错误，请重试");

        $message = $this->msgRep->getInfo($msg_id);
        $member = $message['member'] ?? [];
        unset($message['member']);

        /*
        if (!empty($member)) {
            $member['unlock_funcs'] = MemberUnlockFunc::query()->where(['object_id' => $member['id'], 'uid' => UID])->pluck("object_type");
        }
        */
        $current_member = (new MemberRepository())->getSimpleMember(UID, 3);
        return $this->apiResponse->success([
            'message' => $message,
            'member' => $member,
            'current_member' => $current_member,
//            'unlock_type' => unlock_type()
        ]);
    }

    public function detail()
    {
        $params = request()->all();
        $params['df_uid'] = intval($params['df_uid']);

        $res = $this->msgRep->getList($params, $this->pageSize, $this->pageNum);

        return $this->apiResponse->success($res);
    }

    public function delete()
    {
        $msg_id = request()->get("msg_id");
        if (!$msg_id)
            return $this->apiResponse->error("数据错误，请重试");

        $message = $this->msgRep->getInfo($msg_id);
        if ($message['df_uid'] != UID) {
            return $this->apiResponse->error("无权限");
        }

        Msg::where("id", "=", $message['id'])->update(['deleted_at' => date("Y-m-d H:i:s")]);
        return $this->apiResponse->success([], "success");
    }

    public function get_unread_amount()
    {
        $unread = $this->msgRep->getUnreadAmount(UID);
        return $this->apiResponse->success(['unread_amount' => $unread]);
    }

}
